package com.qianchang.microfinance.view.findings;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.mine.bill.PayTonglianActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/***
 * 导流页面 加载 H5 网页 2018.8.8
 */


public class DiversionActivity extends BaseActivity{

    @BindView(R.id.wv_diversion_id)
    WebView wvDiversion;
    @BindView(R.id.progressBarLoading)
    ProgressBar mLoadingProgress;

    private String url = "https://www.baidu.com/";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diversion_layout);
        ButterKnife.bind(this);

        initView();

    }

    private void initWebView(){

        WebView webView = new WebView(getApplicationContext());
        webView.setVisibility(View.VISIBLE);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("http://www.baidu.com");
    }




    @Override
    protected void initView() {
        super.initView();

        wvDiversion.loadUrl(url);
        WebSettings webSettings = wvDiversion.getSettings();
        webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
//        webSettings.setLoadWithOverviewMode(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);//支持缩放
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        //加载第三方页面时，我们没办法控制别人使用http还是https，如果我们服务器和对方使用不同的协议，就需要做这个处理了。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        // 加载需要显示的网页
//        webView.addJavascriptInterface(new JavascriptInterface(), "classNameBeExposedInJs");
        wvDiversion.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        wvDiversion.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (mLoadingProgress != null) {
                    mLoadingProgress.setProgress(newProgress);
                    if (newProgress == 100) mLoadingProgress.setVisibility(View.GONE);
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        /*******************************/
        wvDiversion.setWebViewClient(new WebViewClient(){


            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                String url = request.getUrl().toString();
                try{
                    if(!url.startsWith("http://") && !url.startsWith("https://")){
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        return false;
                    }
                }catch (Exception e){//防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
                    return false;//没有安装该app时，返回true，表示拦截自定义链接，但不跳转，避免弹出上面的错误页面
                }
                wvDiversion.loadUrl(String.valueOf(request.getUrl()));
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

        });

        webSettings.setJavaScriptEnabled(true);

    }


    @Override
    protected void initData() {
        super.initData();

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (wvDiversion.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK){//点击返回按钮的时候判断有没有上一页
//            wvDiversion.goBack(); // goBack()表示返回webView的上一页面
//            return true;
//        }
        return super.onKeyDown(keyCode,event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(wvDiversion != null){
            wvDiversion.destroy();
            wvDiversion = null;
        }
    }
}
